;(function($){
	$.frontspa_brzd = $.frontspa_brzd || {};
	$.fn.frontspa_brzd = function(opts){
		var el = this;
		var opt = {
			ct_brzd : [],
			cbbh : [],
			wait : function(){},
			goon : function(){},
			dataBack : function(){},
			closeBack : function(){},
			selBack : function(){},
			loadXmCbBack : function(){},
		}
		var param = $.extend(opt,opts);
		var app = new App(el,param);
		$.frontspa_brzd = $.extend($.frontspa_brzd,app)
	}
	
	var App = function(el,param){
		this.el = el;
		this.param = param;
		this.initHtml();
		this.initBrzdGrid();
//		this.initCbbhGrid();
	}
	
	App.prototype = {
		bindEvent : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
		},
		initHtml : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			var html = 	'<div id="brzdList" class="widget-box">'+
							'<div class="widget-header">'+
								'<h5 class="widget-title">'+
									$.i18n.propJsp("手牌列表")+
								'</h5>'+
							'</div>'+
							'<div class="widget-body">'+
								'<table id="brzdGrid"></table>'+
							'</div>'+
						'</div>';
//						'<div id="brzdDcode" class="widget-box">'+
//							'<div class="widget-header">'+
//								'<h5 class="widget-title">'+
//									$.i18n.propJsp("科目列表")+
//								'</h5>'+
//							'</div>'+
//							'<div class="widget-body">'+
//								'<table id="cbbhGrid"></table>'+
//							'</div>'+
//						'</div>';
			el.html(html);
			t.setSize();
		},
		setSize : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			var winh = $(window).height();
			var top = el.offset().top;
			var h = (winh - top)  - 100;
			el.find("#brzdList").css({"height":h});
			el.find("#brzdDcode").css({"height":h});
		},
		initBrzdGrid : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			var grid_selector = el.find("#brzdGrid");
			var h =  el.find("#brzdList").height() - 39 - 42 - 21
			grid_selector.jqGrid({
				datatype:"local",
				height:h,
				data :[],
				width:el.find("#brzdList").width(),
				colNames:['NO','手牌','金额','手牌（原）','金额（原）','贷方金额（原）','sele','th_info','status_info','info','info1',''],
				colModel:
					[{
						name:'no',
						index:'no',
						width:100,
						key:true,
						hidden:true
					},{
						name:'th_status',
						index:'th_status',
						width:60,
						formatter:function(cellvalue, options, rowObject){
							var th_info = JSON.parse(rowObject.th_info)
							var th_info_text = ""
							$.each(th_info,function(key,value){
								if(JSON.stringify(value)!="{}"){
									th_info_text += getTextStyle(value);
								}
							})
							
							var status_info = JSON.parse(rowObject.status_info)
							var status_info_text = ""
							$.each(status_info,function(key,value){
								if(JSON.stringify(value)!="{}"){
									status_info_text += getTextStyle(value);
								}
							})
								
							var str = 	'<div style="margin: -3px -4px 0px -4px;padding:0px 4px 3px 4px;border-bottom: 1px solid #E1E1E1;height:24px">'+
											th_info_text+
										'</div>'+
										'<div style="margin: 0px -4px -3px -4px;padding:3px 4px 0px 4px;height:23px">'+
											status_info_text+
										'</div>';
							return str
						}
					},{
						name:'xfje_df',
						index:'xfje_df',
						width:50,
						align : "right",
						formatter : "number",
						thousandsSeparator : ",",
						decimalPlaces : 2,
						formatter:function(cellvalue, options, rowObject){
							
							var str = ""
							if(Number(rowObject.dfje)!=0){
								str = 	'<div style="margin: -3px -4px 0px -4px;padding:0px 4px 3px 4px;height:24px;font-weight: bold;">'+
											rowObject.xfje+
										'</div>'+
										'<div style="margin: 0px -4px -3px -4px;padding:3px 4px 0px 4px;height:23px;">'+
											'<span class="" style="color:#d15b47 !important;font-weight: bold;">'+rowObject.dfje+'</span>'+
										'</div>';
							}else{
								str = 	'<div style="font-weight: bold;">'+
											rowObject.xfje+
										'</div>';
							}
										
							return str
						}
					},{
						name:'th',
						index:'th',
						width:100,
						hidden:true
					},{
						name:'xfje',
						index:'xfje',
						width:50,
						align : "right",
						formatter : "number",
						thousandsSeparator : ",",
						decimalPlaces : 2,
						hidden:true
					},{
						name:'dfje',
						index:'dfje',
						width:50,
						align : "right",
						formatter : "number",
						thousandsSeparator : ",",
						decimalPlaces : 2,
						hidden:true
					},{
						name:'sele',
						index:'sele',
						width:100,
						hidden:true
					},{
						name:'th_info',
						index:'th_info',
						width:100,
						hidden:true
					},{
						name:'status_info',
						index:'status_info',
						width:100,
						hidden:true
					},{
						name:'info',
						index:'info',
						width:100,
						hidden:true
					},{
						name:'info1',
						index:'info1',
						width:100,
						hidden:true
					},{
						name:'custom',
						index:'custom',
						width:300,
						formatter:function(cellvalue, options, rowObject){
							var info = JSON.parse(rowObject.info)
							var info1 = JSON.parse(rowObject.info1)
							
							var info_text = ""
							$.each(info,function(key,value){
								if(JSON.stringify(value)!="{}"){
									info_text += getTextStyle(value);
								}
							})
							
							var info1_text = ""
							$.each(info1,function(key,value){
								if(JSON.stringify(value)!="{}"){
									info1_text += getTextStyle(value);
								}
							})
							
							var str = 	'<div style="margin: -3px -4px 0px -4px;padding:0px 4px 3px 4px;height:24px">'+
											info_text+
										'</div>'+
										'<div style="margin: 0px -4px -3px -4px;padding:3px 4px 0px 4px;height:23px">'+
											info1_text+
										'</div>';
							return str;
						}
					}],
				sortname : "th",
				sortorder : "asc",
				shrinkToFit:true,
				multiselect:true, 
				multiboxonly:true,
				footerrow : true,
				onSelectAll : function(){
					t.selBackParam();
				},
			    onSelectRow:function(rowid){
			    	t.selBackParam();
		        },
	        	ondblClickRow: function(rowid){ 
	        	},
			});
//			t.freshBrzdGrid();
		},
		freshBrzdGrid : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			var grid_selector = el.find("#brzdGrid");
			grid_selector.jqGrid("clearGridData");
			var ds = param.ct_brzd;
			if(ds.length!=0){
				for ( var i = 0; i < ds.length; i++){
					grid_selector.jqGrid('addRowData', ds[i].no, ds[i],"last");
					if(ds[i].sele==1){
						grid_selector.jqGrid('setSelection',ds[i].no);
					}
				}
			}
			var xfje  = grid_selector.getCol('xfje',false,'sum');
			var countNum = grid_selector.jqGrid('getGridParam', 'records')
	      	//将统计数据放到统计行上
//			grid_selector.footerData("set",{xfje:xfje});
			$("#gbox_brzdGrid").find(".footrow td[aria-describedby='brzdGrid_th_status']").text(countNum)
			$("#gbox_brzdGrid").find(".footrow td[aria-describedby='brzdGrid_xfje_df']").text(xfje.toFixed(2))
			//全部加载去掉
//			var allIds = grid_selector.jqGrid('getDataIDs');
//			var nos = allIds.join(",")
//			param.loadXmCbBack(nos,"","loadCbbh");
			//清空科目代码
//			el.find("#cbbhGrid").jqGrid("clearGridData");
		},
//		initCbbhGrid : function(){
//			var el = this.el;
//			var param = this.param;
//			var t = this;
//			
//			var grid_selector = el.find("#cbbhGrid");
//			var h =  el.find("#brzdDcode").height() - 39 - 42 - 21
//			grid_selector.jqGrid({
//				datatype:"local",
//				height:h,
//				data :[],
//				width:el.find("#brzdDcode").width(),
//				colNames:['编号','名称','金额'],
//				colModel:
//					[
//					{name:'bh',index:'bh',width:100,key:true,},
//					{name:'mc',index:'mc',width:100,},
//					{name:'amount',index:'amount',width:100,align:"right",
//						formatter : "number",
//						thousandsSeparator : ",",
//						decimalPlaces : 2,
//					},
//					],
//				sortname : "th",
//				sortorder : "asc",
//				shrinkToFit:true,
//				multiselect:true, 
//				footerrow : true,
//				onSelectAll : function(){
//					t.selBackParam();
//				},
//			    onSelectRow:function(rowid){
//			    	t.selBackParam();
//		        },
//	        	ondblClickRow: function(rowid){ 
//	        	},
//			});
////			t.freshCbbhGrid();
//		},
//		freshCbbhGrid : function(){
//			var el = this.el;
//			var param = this.param;
//			var t = this;
//			var grid_selector = el.find("#cbbhGrid");
////			grid_selector.jqGrid("clearGridData");
//			var ds = param.cbbh;
//			var allIds = grid_selector.jqGrid('getDataIDs');
//			$(allIds).each(function(k,v){
//				grid_selector.jqGrid("setCell",v,"amount",0.00)
//			})
			
//			if(ds.length!=0){
//				for ( var i = 0; i < ds.length; i++){
//					if($.inArray(ds[i].bh,allIds)==-1){
//						grid_selector.jqGrid('addRowData', ds[i].bh, ds[i],"last");
//					}else{
//						grid_selector.jqGrid("setCell",ds[i].bh,"amount",ds[i].amount)
//					}
////					if(ds[i].sele==1){
////						grid_selector.jqGrid('setSelection',ds[i].bh);
////					}
//				}
//			}
//			var amount = grid_selector.getCol('amount',false,'sum');
//	      	//将统计数据放到统计行上
//			grid_selector.footerData("set",{amount:amount});
//		},
		selBackParam : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			var grid_selector_brzd = el.find("#brzdGrid");
//			var grid_selector_cbbh = el.find("#cbbhGrid");
			
			var nos = "";
//			var cbbh = "";
			var allIds = grid_selector_brzd.jqGrid('getDataIDs');
	    	var ids = grid_selector_brzd.jqGrid('getGridParam','selarrrow');
	    	if(ids.length==0){
	    		nos = allIds.join(",");
	    	}else{
	    		nos = ids.join(",");
	    	}
			
//	    	var allIds = grid_selector_cbbh.jqGrid('getDataIDs');
//	    	var ids = grid_selector_cbbh.jqGrid('getGridParam','selarrrow');
//	    	if(ids.length==0){
//	    		cbbh = allIds.join(",");
//	    	}else{
//	    		cbbh = ids.join(",");
//	    	}
	    	param.selBack(nos);
		},
	}
	
	
	
	function getContextPath(){
		var pathName = document.location.pathname;
		var index = pathName.substr(1).indexOf("/");
		var path = pathName.substr(0, index + 1);
		return path;
	}
})(jQuery);